import requests
from requests.models import Request
import urllib3
from urllib3.exceptions import InsecureRequestWarning 
urllib3.disable_warnings(InsecureRequestWarning)
from modules.file_module import file_moduler


class Scanner_request:
    def __init__(self) -> None:
        self.headers = {
            "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like)",
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        self.proxy = {
            "http": "http://127.0.0.1:8080"
        }
        self.timeout = 10
        self.logger = file_moduler()

    def logger_process(self, target, result, is_vulnerable):
        if result == 200 and is_vulnerable:
            save_value = f"\[VULNERABLE]{target}\",\"{result}\"\n"
            print(f"[*] VULNERABLE: {target}:Status Code: {result}")
            self.logger.save_value_file(save_value, 'vulnerable.log')
        else:
            save_value = f"\[ERROR]{target}\",\"{result}\"\n"
            print(f"[X] {target}:{result}")
            self.logger.save_value_file(save_value, 'error.log')

    def send_request(self, url):
        """ verify if root:x:0: at response """
        try:
            response = requests.get(url, verify=False, headers=self.headers, timeout=self.timeout)
            if "root:x:0" in response.text or "www-data:x" in response.text or "ntp:x=" in response.text:
                is_vulnerable = True
                self.logger_process(url, response.status_code, is_vulnerable)
            else:
                is_vulnerable = False
                self.logger_process(url, response.status_code, is_vulnerable)
        except (requests.exceptions.Timeout, requests.exceptions.HTTPError) as e:
            print(f"[ERROR] {e}")
